package 笔试;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import util.LogUtils;
import util.TraverseUtils;

public class ComparatorDemo {
    public List<Student> mList;  
    public List<Comparator<Student>> mCmpList = new ArrayList<Comparator<Student>>();  
    public ComparatorDemo(List<Student> list){  
        mList = list;  
        mCmpList.add(compareAgeASC);  
        mCmpList.add(comparePointDESC);  
        sort(mList, mCmpList);  
    }  
    public void sort(List<Student> list, final List<Comparator<Student>> comList) {  
        if (comList == null)  
            return;  
        Comparator<Student> cmp = new Comparator<Student>() {  
            @Override  
            public int compare(Student o1, Student o2) {  
                for (Comparator<Student> comparator : comList) {  
                    if (comparator.compare(o1, o2) > 0) {  
                        return 1;  
                    } else if (comparator.compare(o1, o2) < 0) {  
                        return -1;  
                    }  
                }  
                return 0;  
            }  
        };  
        Collections.sort(list, cmp);  
        TraverseUtils.printlnOfList(list);
    }  
  
    private Comparator<Student> compareAgeASC = new Comparator<ComparatorDemo.Student>() {  
  
        @Override  
        public int compare(Student o1, Student o2) {  
            return o1.age > o2.age ? 1 : -1;  
        }  
    };  
  
    private Comparator<Student> comparePointDESC = new Comparator<ComparatorDemo.Student>() {  
  
        @Override  
        public int compare(Student o1, Student o2) {  
            return o1.point < o2.point ? 1 : -1;  
        }  
    };  
  
    /** 
     * @author 80059130 
     *  
     */  
   public static class Student {  
        public int age;  
        public String name;  
        public int point;
		public Student(int age, String name, int point) {
			super();
			this.age = age;
			this.name = name;
			this.point = point;
		}
		@Override
		public String toString() {
			return "Student [age=" + age + ", name=" + name + ", point="
					+ point + "]";
		}

    }  
    
    public static void main(String[] args) {
    	List<Student> list = new ArrayList<ComparatorDemo.Student>();
    	list.add(new Student(1, "name1", 1));
     	list.add(new Student(1, "name2", 2));
     	list.add(new Student(2, "name3", 3));
     	list.add(new Student(4, "name4", 4));
     	list.add(new Student(4, "name5", 5));
     	list.add(new Student(6, "name6", 6));
     	list.add(new Student(7, "name7", 7));
     	list.add(new Student(8, "name8", 8));
     	list.add(new Student(9, "name9", 9));
    	ComparatorDemo demo = new ComparatorDemo(list);
	}
}
